<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use ReflectionFunction;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Card extends Backend
{

    /**
     * card模型对象
     * @var \app\admin\model\card
     */
    protected $model = null;


//    protected $noNeedRight = ['table1', 'table2', 'table3'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Card();

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $status = json_decode($this->request->request('filter'), true);
            $map = [];
            if (!isset($status['status']) || !$status['status']) {
                $map['card.status'] = ['>', 0];
            } else {
                $map['card.status'] = ['=', $status['status']];
            }

            $func = new ReflectionFunction($where);
            $where = $func->getStaticVariables();
            foreach ($where['where'] as $key => $val) {
                if ($val[0] == "card.status") {
                    unset($where['where'][$key]);
                }
                if ($val[0] == "card.name") {
                    $map['card.name'] = ['like', '%' . $val[2] . '%'];
                }
                if ($val[0] == "card.type") {
                    $map['card.type'] = ['=', $val[2]];
                }
                if ($val[0] == "card.number") {
                    $map['card.number'] = ['like', '%' . $val[2] . '%'];
                }
                if ($val[0] == "card.create_time") {
                    $map['card.create_time'] = ['BETWEEN TIME', $val[2]];
                }
                if ($val[0] == "card.username") {
                    $map['card.username'] = ['like', '%' . $val[2] . '%'];
                }
                if ($val[0] == "address.phone") {
                    $map['address.phone'] = ['like', '%' . $val[2] . '%'];
                }
            }

            $list = $this->model
                ->with(['address'])
                ->where($map)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                switch ($row['status']) {
                    case 1;
                        $row['status'] = '待支付';
                        break;
                    case 2;
                        $row['status'] = '待发货';
                        break;
                    case 3;
                        $row['status'] = '待收货';
                        break;
                    case 4;
                        $row['status'] = '退款中';
                        break;
                    case 5;
                        $row['status'] = '已完成';
                        break;
                    case 6;
                        $row['status'] = '已退款';
                        break;
                    case 7;
                        $row['status'] = '已取消';
                        break;
                }
                switch ($row['type']) {
                    case 1;
                        $row['type'] = '余额支付';
                        break;
                    case 2;
                        $row['type'] = '微信支付';
                        break;
                    case 3;
                        $row['type'] = '爱心值';
                        break;
                }
                $row['money'] = $row['money'] > 1 ? $row['money'] * $row['num'] . '.00' : $row['money'];
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('dd', 1);
        return $this->view->fetch();
    }


    /*
     * 订单详情
     */
    public function details($ids = "")
    {
        $row = $this->model->get(['id' => $ids])->toArray();

        switch ($row['status']) {
            case 1;
                $row['status'] = "待支付";
                break;
            case 2;
                $row['status'] = "已支付";
                break;
            case 3;
                $row['status'] = "待发货";
                break;
            case 4;
                $row['status'] = "退款中";
                break;
            case 5;
                $row['status'] = "已完成";
                break;
            case 6;
                $row['status'] = "已退款";
                break;
            case 7;
                $row['status'] = "已取消";
                break;
        }
        switch ($row['type']) {
            case 1;
                $row['type'] = "余额支付";
                break;
            case 2;
                $row['type'] = "微信支付";
                break;
            case 3;
                $row['type'] = "积分";
                break;
        }
        $url = config('fastadmin.api_url');

        $img = Db::name('goods')->where('id', $row['goods_id'])->value('image');
        $row['img'] = $img ? $url . $img : "";
        $row['child'] = "";

        $row['money'] = $row['money'] * $row['num'];
        $address = Db::name('user_address')->where('id', $row['address_id'])->field('phone,provincial,city,area,address')->find();
        $row['address'] = $address['provincial'] . ' ' . $address['city'] . ' ' . $address['area'] . ' ' . $address['address'];
        $row['phone'] = $address['phone'];
        $this->view->assign("row", $row);
        return $this->view->fetch();

    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $status = Db::name('goods')->where('id',$ids)->value('status');
//            if($status != 1)$this->error(__('该订单已发货或者待支付状态'));

            $goods = Db::name('vip_goods_order')->where('id',$ids)->update(['oddnumbers'=>$params['oddnumbers'],'express'=>$params['express'],'ex_time'=>time(),'status'=>3]);
            if($goods)
            {
                $this->success(__('添加成功'));
            }else{
                $this->error(__('添加失败'));
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }




    /*一键发货*/
    public function multi($ids = "")
    {

        $data = json_decode(json_encode($this->model->where('id', 'in', $ids)->field('id,status')->select()), true);

        if (count($data)) {
            foreach ($data as $key => $val) {
                if ($val['status'] != 2) $this->error('该状态不能确认发货');
            }
        }

        $res = $this->model->where('id', 'in', $ids)->update(['status' => 3]);

        if ($res) {
            $this->success('一键发货成功');
        } else {
            $this->error('一键发货失败');
        }

    }

}
